package yunc.checkData.pojo;

import java.util.HashMap;
import java.util.List;

public class Data021 extends TideData {
    private HashMap<String,Integer> data;

    public Data021() {
    }

    public Data021(HashMap<String, Integer> data) {
        this.data = data;
    }

    public Data021(TitleTide title, List<String> data) {
        HashMap<String,Integer> dataMap = new HashMap<>();
        for (int i = 0; i < data.size(); i++) {
            //解析基础数据
            HashMap<String, Integer> baseDatas = getDataListFromstr(title.getRecordTime(), data.get(i), 5, 5, 12);
            dataMap.putAll(baseDatas);
            //解析高低潮数据
            HashMap<String, Integer> hlDtas = new HashMap<>();
            if (data.get(i).length()==95){
                hlDtas = getHighLowDataListFromstr(title.getRecordTime(), data.get(i), 65, 10, 3);
            }else if (data.get(i).length()==85){
                hlDtas = getHighLowDataListFromstr(title.getRecordTime(), data.get(i), 65, 10, 2);
            }else if (data.get(i).length()==75){
                hlDtas = getHighLowDataListFromstr(title.getRecordTime(), data.get(i), 65, 10, 1);
            }
            dataMap.putAll(hlDtas);
        }
        this.data = dataMap;
    }


    public String getDateFromIndex(String month,String str,int indexAt){

        String day = str.substring(2, 4);
        String dataMark = str.substring(4, 5);
        Integer hour = (Integer.parseInt(dataMark) - 1) * 12 + indexAt;
        String hourStr = hour.toString();
        if (hourStr.length()!=2) {
            hourStr="0"+ hourStr;
        }
        return month + "-" + day + " " + hourStr + ":00:00";
    }

    public String getDateFromHour(String month,String str,int charAt){


        String day = str.substring(2, 4);
        String hour = str.substring(charAt, charAt+2);
        String minute = str.substring(charAt+2, charAt+4);

        return month + "-" + day + " " + hour + ":"+minute+":00";
    }

    public HashMap<String,Integer> getDataListFromstr(String month, String str, int dataStartAt,int dataLength, int dataNum){
        HashMap<String,Integer> datas = new HashMap<>();

        for (int i = 0; i < dataNum; i++) {
            String data = str.substring(dataStartAt + (dataLength * i), dataStartAt + (dataLength * (i + 1)));
            String date = getDateFromIndex(month,str,i);
            datas.put(date,Integer.parseInt(data.replace(" ","")));
        }

        return datas;
    }

    public HashMap<String,Integer> getHighLowDataListFromstr(String month, String str, int dataStartAt,int dataLength, int dataNum){
        HashMap<String,Integer> datas = new HashMap<>();

        for (int i = 0; i < dataNum; i++) {
            String data = str.substring(dataStartAt + (dataLength * i), dataStartAt + (dataLength * (i + 1))).substring(4,10);
            String date = getDateFromHour(month,str,dataStartAt + (dataLength * i));
            datas.put(date,Integer.parseInt(data.replace(" ","")));
        }

        return datas;
    }

    public HashMap<String, Integer> getData() {
        return data;
    }

    public void setData(HashMap<String, Integer> data) {
        this.data = data;
    }

    public static void main(String[] args) {
        String str= "22012  88   77   64   69   96  136  178  201  199  187  160  127 0728  204 1610 - 38 2325  111 ";
        System.out.println(new Data021().getDataListFromstr("2020-08",str,5,5,12));
    }
}
